package com.cangxuan.backend.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cangxuan.backend.entity.OrderForm;
import com.cangxuan.backend.entity.Registration;
import com.cangxuan.backend.service.IRegistrationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * Author: 陈青石
 * 挂单接口
 */
@Api(tags = "挂单相关接口")
@Slf4j
@RestController
@RequestMapping("registration")
public class RegistrationController extends BaseController{

    IRegistrationService registrationService;

    @Autowired
    public void setRegistrationService(IRegistrationService registrationService) {
        this.registrationService = registrationService;
    }

    /**
     * 根据商品ID获取挂单列表
     * @param goodsId
     * @return
     */
    @GetMapping("get/{goodsId}")
    @ApiOperation("【需要登录】根据商品ID获取挂单列表")
    public Object get(@ApiParam("藏品ID") @PathVariable int goodsId) {
        return success("请求成功", registrationService.selectRegistrationsByGoodsId(goodsId));
    }


    /**
     * 对挂单商品创建订单
     * @param registrationList
     * @return
     */
    @PostMapping("updateBuyerId")
    @ApiOperation("【需要登录】对挂单商品进行下单")
    public Object updateBuyerId(@ApiParam("挂单商品列表") @RequestBody List<Registration> registrationList) {
        Map<String,Object> map = registrationService.changeCount(registrationList);

        List<OrderForm> list= (List<OrderForm>) map.get("list");


        JSONObject jsonObject=new JSONObject();

        int sum=0;

        for (OrderForm orderForm : list) {
            sum+=orderForm.getPrice();
        }

        jsonObject.put("list",list);
        jsonObject.put("sum",sum);

        return success(String.valueOf(map.get("message")),jsonObject);
    }

    /**
     * 对订单成功支付
     * @param orderFormList
     * @return
     */
    @PostMapping("pay")
    @ApiOperation("【需要登录】对挂单商品成功支付")
    public Object successPay(@ApiParam("挂单商品列表") @RequestBody List<OrderForm> orderFormList) {
        registrationService.successPay(orderFormList);
        return success("支付成功");
    }

    /**
     * 对订单取消支付
     * @param orderFormList
     * @return
     */
    @PostMapping("cancel")
    @ApiOperation("【需要登录】对挂单商品取消下单")
    public Object cancelPay(@ApiParam("挂单商品列表") @RequestBody List<OrderForm> orderFormList) {
        registrationService.cancelPay(orderFormList);
        return success("取消成功");
    }

    /**
     * 尝试判断订单是否超时
     * @param orderFormList
     * @return
     */
    @PostMapping("overTime")
    @ApiOperation("【需要登录】尝试判断订单是否超时")
    public Object registrationOverTime(@ApiParam("挂单商品列表") @RequestBody List<OrderForm> orderFormList) {
        registrationService.overTime(orderFormList);
        return success("没有超时");
    }


}
